//
//  JiaZDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/30.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "JiaZDetViewController.h"

#import "GXDPView.h"
#import "PinJiaCell.h"
#import "JiaZhenCell.h"
#import "PingJiaViewController.h"
#import "KuLiViewController.h"

#import "LoginViewController.h"
#import "NavViewController.h"
#import "JZPayViewController.h"
#import "SDCycleScrollView.h"
@interface JiaZDetViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>{
    NSMutableArray *array;
    NSMutableArray *datas;
    JiaZhenCell *jiazhencell;
}

@property (nonatomic,strong)UITableView *tableView;
//主要是用来接收用户上一次所选的cell的indexpath
@property (nonatomic,strong) NSIndexPath *lastPath;
@property (nonatomic,copy)NSString *str;//订单号
@property (nonatomic,strong)NSMutableDictionary *dic;

@property (nonatomic,copy)NSString *productId;

@end

@implementation JiaZDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self showNav:@"店铺详情"];
    array = [NSMutableArray array];
    datas = [NSMutableArray array];
    _dic = [NSMutableDictionary dictionary];
    [self setBGColor];
    [self loadData];
    [self loadNum];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-50) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    //_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    //头视图
    GXDPView *view = [[[NSBundle mainBundle] loadNibNamed:@"GXDPView" owner:nil options:nil] lastObject];
    if ([_det isEqualToString:@""]) {
       
        view.frame = CGRectMake(0, 0, ScreenWidth, 270);

    }else{
       
        view.frame = CGRectMake(0, 0, ScreenWidth, 300);

    }
    _tableView.tableHeaderView = view;
    //
//    UIView *v = [WTImageScroll ShowNetWorkImageScrollWithFream:CGRectMake(0, 0, ScreenWidth, 170) andImageArray:_images andBtnClick:^(NSInteger tagValue) {
    
//    }];
    view.images.userInteractionEnabled = YES;
//    [view.images addSubview:v];
    
    SDCycleScrollView *cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth, 170) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    [view.images addSubview:cycleView];
    cycleView.imageURLStringsGroup = _images;

    
    
    //
    CGFloat count = [_star floatValue];
    StarView *starView = [[StarView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    starView.showStar = count * 20;
    [view.starView addSubview:starView];
    
    view.labName.text = _name;
    view.labdet.text = _det;
    view.labPlace.text = [NSString stringWithFormat:@"地址：%@",_place];
    view.labOrder.text = _order;
    
    //尾
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-50, ScreenWidth, 50)];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.5)];
    line.backgroundColor =  COLOR_RGB(230, 230, 230, 1);
    [footView addSubview:line];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(ScreenWidth/4, 7, ScreenWidth/2, 36);
    btn.backgroundColor = COLOR_RGB(253, 17, 66, 1);
    btn.layer.cornerRadius = btn.frame.size.height/2;
    [btn setTitle:@"呼叫家政" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickTEL) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:btn];
    
//    UIView *views = [[UIView alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth/3*2, 50)];
//    views.backgroundColor = [UIColor whiteColor];
//    [footView addSubview:views];
    
    /*
    UILabel *lab1 = [[UILabel alloc] init];
    lab1.text = _name;
    lab1.font = [UIFont systemFontOfSize:13.0];
    [views addSubview:lab1];
    [lab1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(views.left).offset(16);
        make.top.equalTo(views.top).offset(8);
    }];
    
    UILabel *lab2 = [[UILabel alloc] init];
    lab2.text = _place;
    lab2.textColor = [UIColor darkGrayColor];
    lab2.numberOfLines = 0;
    lab2.font = [UIFont systemFontOfSize:11.0];
    [views addSubview:lab2];
    [lab2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(views.left).offset(16);
        make.top.equalTo(lab1.bottom).offset(0);
        make.right.equalTo(btn.left);
    }];*/
    
}

#pragma mark -- cycleDelegate
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
}

//呼叫家政
- (void)clickTEL{
    if (![user objectForKey:@"userid"]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        NavViewController *nav = [[NavViewController alloc] initWithRootViewController:login];
        nav.navigationBarHidden = YES;
        [self presentViewController:nav animated:YES completion:nil];
        return;
    }
    
    if (_productId.length == 0) {
        
        [LICGToast showWithText:@"请选择服务内容"];
        return;
    }
    JZPayViewController *pay = [[JZPayViewController alloc] init];
    pay.name = _name;
    pay.Servename = [_dic objectForKey:@"name"];
    pay.price = [NSString stringWithFormat:@"%@",[_dic objectForKey:@"price"]];
    pay.merid = _merid;
    pay.productid = _productId;
    [self.navigationController pushViewController:pay animated:YES];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return array.count;
    }
    return datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *cellId = @"Cell1";
        jiazhencell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!jiazhencell) {
            jiazhencell = [[[NSBundle mainBundle] loadNibNamed:@"JiaZhenCell" owner:nil options:nil] lastObject];
        }
        jiazhencell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *dic = array[indexPath.row];
        jiazhencell.labName.text = [dic objectForKey:@"name"];
        jiazhencell.labDet.text = [dic objectForKey:@"synopsis"];
        jiazhencell.labPrice.text = [NSString stringWithFormat:@"￥%.2f%@",[[dic objectForKey:@"price"] floatValue],dic[@"timetype"]];
        NSInteger row = indexPath.row;
        NSInteger oldRow = _lastPath.row;
        if (row == oldRow && _lastPath != nil) {
            jiazhencell.Images.image = [UIImage imageNamed:@"勾"];
        }else{
            jiazhencell.Images.image = [UIImage imageNamed:@"椭圆-2-拷贝"];
        }
        
        return jiazhencell;
    }else{
        static NSString *cellId = @"Cell2";
        PinJiaCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"PinJiaCell" owner:nil options:nil] lastObject];
        }
        cell.dataDic = datas[indexPath.row];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        NSDictionary *dic = datas[indexPath.row];
//        cell.labName.text = [dic objectForKey:@"nickname"];
//
//        NSString *time = [dic objectForKey:@"commenttime"];
//        NSString *date = [self getNewDate:time];
//        cell.labDate.text = date;
//
//        cell.labText.text = [dic objectForKey:@"comment"];
//        NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"avatar"]];
//        cell.images.layer.cornerRadius = 11.5;
//        cell.images.layer.masksToBounds = YES;
//        [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"TIM图片20170810135854"]];
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 60.0;
    }
    return 100.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _dic = array[indexPath.row];
    _productId = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"id"] integerValue]];
    if (indexPath.section == 0) {
        NSInteger newrow = indexPath.row;
        NSInteger oldRow = (self.lastPath != nil)?self.lastPath.row:-1;
        if (newrow != oldRow) {
            jiazhencell = [tableView cellForRowAtIndexPath:indexPath];
            jiazhencell.Images.image = [UIImage imageNamed:@"勾"];
            
            jiazhencell = [tableView cellForRowAtIndexPath:self.lastPath];
            jiazhencell.Images.image = [UIImage imageNamed:@"椭圆-2-拷贝"];
            self.lastPath = indexPath;
        }
        NSDictionary *dic = array[indexPath.row];
        NSLog(@"name:%@",[dic objectForKey:@"name"]);
        
    }else{
        
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *labs = [[UILabel alloc] initWithFrame:CGRectMake(16, 12, 4, 16)];
    labs.backgroundColor = [UIColor blueColor];
    [view addSubview:labs];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(36, 10, 100, 20)];
    lab.text = @"店铺评价";
    lab.textColor = COLOR_RGB(77, 77, 77, 1);
    lab.font = [UIFont systemFontOfSize:15.0];
    [view addSubview:lab];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, 0.5)];
    line.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [view addSubview:line];
    
    UILabel *labpl = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-100-16, 10, 100, 20)];
    NSInteger indexs = datas.count;
    labpl.text = [NSString stringWithFormat:@"总评价：%li",indexs];
    labpl.font = [UIFont systemFontOfSize:14.0];
    labpl.textColor = [UIColor darkGrayColor];
    labpl.textAlignment = NSTextAlignmentRight;
    [view addSubview:labpl];
    if (section == 0) {
        labpl.hidden = YES;
        lab.text = @"精选服务";
    }
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}

//尾视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10.0)];
        view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
        
        return view;
    }else{
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
        view.backgroundColor = [UIColor whiteColor];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, ScreenWidth, 40.0);
        [btn setTitle:@"查看全部评价" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:12.0];
        [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2+35, 14, 60, 12)];
        NSInteger indexs = datas.count;
        lab.text = [NSString stringWithFormat:@" (%li)",indexs];
        lab.font = [UIFont systemFontOfSize:12.0];
        lab.textColor = [UIColor redColor];
        [view addSubview:lab];
        
        return view;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 10.0;
    }
    return 40.0;
}

- (void)click{
    PingJiaViewController *pingjia = [[PingJiaViewController alloc] init];
    pingjia.merid = _merid;
    pingjia.url = [NSString stringWithFormat:@"%@/App/Merchantcomment/housekeeping",urlHead];
    [self.navigationController pushViewController:pingjia animated:YES];
}
//生成订单号
- (void)loadNum{
    NSString *url = [NSString stringWithFormat:@"%@/App/Shoppingcart/ordernumber",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        _str = [responseObject objectForKey:@"result"];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

//家政详情
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Housekeeping/houservice",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"家政详情res:%@",responseObject);
        array = [[responseObject objectForKey:@"result"] objectForKey:@"service"];
        datas = [[[responseObject objectForKey:@"result"] objectForKey:@"comment"] objectForKey:@"comment"];
        [_tableView reloadData];
        [self loadUI];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        
        [SVProgressHUD dismiss];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
